package com.iplant.pdm.serviceImpl.dao.fpc;

import com.iplant.pdm.service.po.fpc.FPCPart;
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class FPCPartDAO extends BaseDAO {

	private static FPCPartDAO Instance = null;
	private static Logger logger = LoggerFactory.getLogger(FPCPartDAO.class);

	private FPCPartDAO() {
		super();

		setTableName("fpc_part");
	}

	public static FPCPartDAO getInstance() {
		if (Instance == null)
			Instance = new FPCPartDAO();
		return Instance;
	}

	public void FPC_UpdatePart(BMSEmployee wLoginUser, FPCPart wPart, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wPart == null || StringUtils.isEmpty(wPart.Name) || StringUtils.isEmpty(wPart.Code)) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			FPCPart wPartO = this.FPC_CheckPart(wLoginUser, wPart, wErrorCode);
			if (wPartO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("Name", wPart.Name);
			wParams.put("Code", wPart.Code);
			wParams.put("Description", wPart.Description);

			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wPart.Active);

			if (wPart.ID > 0) {
				wParams.put("ID", wPart.ID);
				super.Update(super.getTableName("fpc_part"), "ID", wParams);
			} else {
				wParams.put("CreatorID", wLoginUser.getID());
				wParams.put("CreateTime", Calendar.getInstance());
				wPart.ID = super.Insert(super.getTableName("fpc_part"), wParams);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_AddPart", ex);
		}
	}

	public void FPC_ActivePart(BMSEmployee wLoginUser, FPCPart wPart, int wActive, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wPart == null || wPart.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParams = new HashMap<String, Object>();

			wParams.put("ID", wPart.ID);
			wParams.put("EditorID", wLoginUser.getID());
			wParams.put("EditTime", Calendar.getInstance());
			wParams.put("Active", wActive);
			super.Update(super.getTableName("fpc_part"), "ID", wParams);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("FPC_ActivePart", ex);
		}
	}

	public List<FPCPart> FPC_QueryPartList(BMSEmployee wLoginUser, String wName, int wActive, Pagination wPagination,
			OutResult<Integer> wErrorCode) {
		return this.FPC_QueryPartList(wLoginUser, -1, "", wName, wActive, wPagination, wErrorCode);
	}

	private List<FPCPart> FPC_QueryPartList(BMSEmployee wLoginUser, int wID, String wCode, String wName, int wActive,
			Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<FPCPart> wFPCPartList = new ArrayList<FPCPart>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format(
					"Select t.*,t3.Name as CreatorName ,t4.Name as EditorName  from {0}.fpc_part t  "
							+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
							+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
							+ " where (:ID<=0 or  t.ID=:ID) and (:Code =''''  or  t.Code=:Code)"
							+ " AND (:Active<0 or  t.Active=:Active) " + " AND (:Name = '''' or  t.Name like :Name)",
					super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParams.put("ID", wID);
			wParams.put("Code", wCode);
			wParams.put("Active", wActive);
			wParams.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

			wFPCPartList = super.QueryForList(wSQLText, wParams, wPagination, FPCPart.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryPartList", ex);
		}
		return wFPCPartList;
	}

	public FPCPart FPC_QueryPart(BMSEmployee wLoginUser, int wID, String wCode, OutResult<Integer> wErrorCode) {
		FPCPart wResult = new FPCPart();
		wErrorCode.set(0);

		try {
			List<FPCPart> wList = null;
			if (wID > 0) {
				wList = this.FPC_QueryPartList(wLoginUser, wID, "", "", -1, Pagination.getDefault(), wErrorCode);

			} else if (StringUtils.isNotEmpty(wCode)) {
				wList = this.FPC_QueryPartList(wLoginUser, -1, wCode, "", -1, Pagination.getDefault(), wErrorCode);
			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_QueryPartByID", ex);
		}
		return wResult;
	}

	public FPCPart FPC_CheckPart(BMSEmployee wLoginUser, FPCPart wFPCPart, OutResult<Integer> wErrorCode) {
		FPCPart wPart = new FPCPart();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParams = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.fpc_part   where ID!=:ID and ( :Code != '''' and Code=:Code) ",
					super.getDBName());
			wParams.clear();
			wParams.put("ID", wFPCPart.ID);
			wParams.put("Name", wFPCPart.Name);
			wParams.put("Code", wFPCPart.Code);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParams);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wPart.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wPart.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wPart.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

				wPart.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wPart.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wPart.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wPart.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wPart.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("FPC_CheckPart", ex);
		}
		return wPart;
	}

}
